package com.booksystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booksystem.common.RestResult;
import com.booksystem.common.ResultCodeConstant;
import com.booksystem.dto.PromotionCouponDTO;
import com.booksystem.dto.PromotionCouponDeleteDTO;
import com.booksystem.dto.PromotionCouponQueryDTO;
import com.booksystem.dto.PromotionCouponUpdateStatusDTO;
import com.booksystem.entity.PromotionCouponDO;
import com.booksystem.service.PromotionCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   促销活动管理控制器
 * </p>
 * @author 段伟豪
 */
@Api("促销活动管理")
@RequestMapping("/promotion/coupon")
@RestController
@Validated
public class PromotionCouponController {

    @Autowired
    private PromotionCouponService promotionCouponService;

    /**
     * 新增优惠券
     *
     * @param dto 优惠券信息
     * @return 结果
     */
    @PostMapping("/add")
    @ApiOperation("新增优惠券")
    public RestResult<Boolean> addPromotionCoupon(@RequestBody @Valid PromotionCouponDTO dto) {
        Boolean result = promotionCouponService.addPromotionCoupon(dto);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }

    /**
     * 查询优惠券列表
     *
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/list")
    @ApiOperation("查询优惠券列表")
    public RestResult<Page<PromotionCouponDO>> listPromotionCoupons(@Valid PromotionCouponQueryDTO queryDTO) {
        Page<PromotionCouponDO> result = promotionCouponService.listPromotionCoupons(queryDTO);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }

    /**
     * 更新优惠券状态
     *
     * @param dto 更新参数
     * @return 结果
     */
    @PutMapping("/update-status")
    @ApiOperation("更新优惠券状态")
    public RestResult<Boolean> updatePromotionCouponStatus(@RequestBody @Valid PromotionCouponUpdateStatusDTO dto) {
        Boolean result = promotionCouponService.updatePromotionCouponStatus(dto);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }

    /**
     * 删除优惠券
     *
     * @param dto 删除参数
     * @return 结果
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除优惠券")
    public RestResult<Boolean> deletePromotionCoupon(@RequestBody @Valid PromotionCouponDeleteDTO dto) {
        Boolean result = promotionCouponService.deletePromotionCoupon(dto);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
    }
}